﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ExtendedDal;


namespace WMS
{
	public partial class SearchExamineDoc : Form
	{
		public string SelectedNumber { get; set; }
        public int SelectedSupplyOrderId { get; set; }
        public int SelectedTransferRequestId { get; set; }

        public int InputSupplyOrderId { get; set; }
        public int InputTransferRequestId { get; set; }

        private bool _isTransfer;

        public SearchExamineDoc()
		{
			InitializeComponent();
		}


        public SearchExamineDoc(bool isTransfer)
        {
            InitializeComponent();
            _isTransfer = isTransfer;
        }

        public SearchExamineDoc(int id, bool isTransfer)
        {
            InitializeComponent();
            if (isTransfer)
                InputTransferRequestId = id;
            else
                InputSupplyOrderId = id;

            _isTransfer = isTransfer;
        }

        private void RunBtn_Click(object sender, EventArgs e)
        {
            using (var context = new ExtendedDataContext())
            {
                var criteria = new List<System.Linq.Expressions.Expression<Func<ExamineDoc, bool>>>();
                
                if (SearchNumberCB.Checked)
                {
                    var serialEntry = string.IsNullOrEmpty(NumberTB.Text) ? "" : NumberTB.Text.Trim();
                    criteria.Add(x => x.Serial == serialEntry);
                }

                if(SearchDateCB.Checked)
                {
                    var dateFrom = DateFromDTP.Value;
                    criteria.Add(x => x.CreationDate >= dateFrom);

                    var dateTo = DateToDTP.Value;
                    criteria.Add(x => x.CreationDate <= dateTo);
                }
                // Filter by supply order id to get only related to this Tawreed order
                if(InputSupplyOrderId > 0)
                {
                    criteria.Add(x => x.SupplyOrderId == InputSupplyOrderId);
                }

                if (InputTransferRequestId > 0)
                {
                    criteria.Add(x => x.TransferRequestId == InputTransferRequestId);
                }

                if (_isTransfer)
                {
                    criteria.Add(x => x.SupplyOrderId == null);
                }
                else 
                {
                    criteria.Add(x => x.TransferRequestId == null);
                }
                IQueryable<ExamineDoc> queryable = context.ExamineDocs.AsQueryable();
                queryable = criteria.Aggregate(queryable, (current, criterion) => current.Where(criterion));

                var docs = queryable.ToList().Select(x => new
                                                              {
                                                                  SupplyOrderId = x.SupplyOrderId ?? x.TransferRequestId,
                                                                  Serial = x.Serial,
                                                                  //OperationName = x.OperationName,
                                                                  CreationDate = x.CreationDate,
                                                                  Matching = x.IsMatched.HasValue && x.IsMatched.Value
                                                                              ? "مطابقة"
                                                                              : "غير مطابقة",
                                                                  Acceptance =
                                                              x.IsAccepted.HasValue && x.IsAccepted.Value
                                                                  ? "نعم"
                                                                  : "لا",
                                                              }).ToList();

                dataGridView1.DataSource = docs;

                if (docs.Count > 0)
                {
                    dataGridView1.Rows[0].Selected = true;
                }
            }
        }

	    private void OkBtn_Click(object sender, EventArgs e)
		{
			if (dataGridView1.SelectedRows.Count > 0)
			{
				SelectedNumber = dataGridView1.SelectedRows[0].Cells["Serial"].Value.ToString();
                SelectedSupplyOrderId = (int)dataGridView1.SelectedRows[0].Cells["SupplyOrderId"].Value;
			}

			this.DialogResult = DialogResult.OK;

			this.Close();
		}

		private void SearchNumberCB_CheckedChanged(object sender, EventArgs e)
		{
			NumberTB.Enabled = SearchNumberCB.Checked;
		}

		private void SearchDateCB_CheckedChanged(object sender, EventArgs e)
		{
			DateFromDTP.Enabled = SearchDateCB.Checked;
			DateToDTP.Enabled = SearchDateCB.Checked;
		}

	}
}
